package daphne.noterun.utilities 
{
	import flash.display.DisplayObject;
	import flash.geom.Rectangle;
	import flash.geom.Point;
	
	/**
	 * ...
	 * @author ...
	 */
	public class Utilities 
	{
		
		public static function clamp(value:Number, low:Number, high:Number)
		{
			if (value < low)
				value = low;
			if (value > high)
				value = high;
			return value;
		}
		
		/**
		 * Use this to avoid floating point precision problems
		 * when checking if two values are equal. Checks if the 
		 * values are close enough to each other. 
		 * @param	num1
		 * @param	num2
		 * @param	threshold
		 */
		public static function aboutEquals(num1:Number, num2:Number, threshold:Number = 1)
		{
			return Math.abs(num1 - num2) <= threshold;
		}
		
		//A simple collision test between two rectangles
		public function collisionRect(bounds1:Rectangle, bounds2:Rectangle, xThreshold:int, yThreshold:int):Rectangle
		{
			bounds1.x += xThreshold;
			bounds1.width -= xThreshold;
			
			bounds1.y += yThreshold;
			bounds1.height -= yThreshold;
			
			return bounds1.intersection(bounds2);
		}
		
		public static function squareDistance(x1:Number, y1:Number, x2:Number, y2:Number):Number
		{
			return Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2);
		}
		
		
		public static function scaleAroundPoint (object:DisplayObject, offsetX:Number, offsetY:Number, absScaleX:Number, absScaleY:Number ):void {
			var relScaleX:Number = absScaleX / object.scaleX;
			var relScaleY:Number = absScaleY / object.scaleY;
			var AC:Point = new Point( offsetX, offsetY );
			AC = object.localToGlobal( AC );
			AC = object.parent.globalToLocal( AC );
			var AB:Point = new Point( object.x, object.y );
			var CB:Point = AB.subtract( AC );
			CB.x *= relScaleX;
			CB.y *= relScaleY;
			AB = AC.add( CB );
			object.scaleX *= relScaleX;
			object.scaleY *= relScaleY;
			object.x = AB.x;
			object.y = AB.y;
		}
		
		//public function collisionDO(obj1:DisplayObject, obj2:DisplayObject, xThreshold:int, yThreshold:int):Rectangle
		//{
			//return collisionRect(obj1.getRect(this.stage), obj2.getRect(this.stage), xThreshold, yThreshold);
		//}
	}

}